<?php
/**
 * Created by : PhpStorm
 * User: lihb
 * Date: 2023/6/29
 * Time: 10:40
 */
namespace app\api\logic;
use app\api\model\Area as AreaModel;
use app\api\model\User as UserModel;
use think\facade\Session;
use think\facade\Cache;
class User{

    private static $userID;
    private static $userType;
    private static $jti;
    private static $iat;

    public function __construct()
    {
        self::$userID=Session::get('userId');
        self::$userType=Session::get('userType');
        self::$jti=Session::get('jti');
        self::$iat=Session::get('iat');
    }
    public function login($data){
        if($data['type']==1){
            $res=Common::checkCode($data,config('data.code_type.login'),config('data.role_type.user'));
            if($res['req']==0){
                return $res;
            }
        }

        $result=[];
        $info=UserModel::where(['email'=>$data['email'],'status'=>1])->field('username,status,id,salt,password,country,avatar')->find();
        if(empty($info)){
            if($data['type']==1){
                $result['scene']=2; //注册场景
                $result['req']=1;
            }else{
                //密码登录未注册账户
                $result['req']=0;
                $result['msg']='Account or password error';
            }
        }else{
            //单独处理已注册未完善资料情况
            if(empty($info['username'])){
                $result['scene']=2; //注册场景
                $result['req']=1;
                $result['id']=$info['id'];
                return $result;
            }
            if($data['type']==2){
                //验证密码
                if($info['password']!=sha1($info['salt'].$data['password'])){
                    $result['req']=0;
                    $result['msg']='Account or password error';
                    return $result;
                }
            }
            //验证状态
            if($info['status']==-1){
                $result['req']=0;
                $result['msg']='The current user has been logged out';
                return $result;
            }
            //获取登录信息
            $result['scene']=1; //登录场景
            $result['req']=1;
            $result['id']=$info['id'];
            $result['username']=$info['username'];
            $result['avatar']=$info['avatar'];

            //提前存入用户session信息
            Session::set('userId',$info['id']);
            Session::set('userType',config('data.role_type.user'));
            Session::set('country',$info['country']);
        }
        return $result;
    }

    public function complete($data){
        if(isset($data['password'])){
            $data['salt']=salt();
            $data['password']=sha1($data['salt'].$data['password']);
            unset($data['repassword']);
            $data['create_time']=date('Y-m-d H:i:s');
        }
        //随机分配头像
        $index=rand(0,3);
        $data['avatar']=config('data.user_avatar_default')[$index];
        $data['id']= UserModel::insertGetId($data);
        return $data;
//        return $userModel->where(['id'=>session(['userId'])])->update($data);
    }

    public function edit($data){
        $userID=self::$userID;
        //头像上传
//        $file=request()->file('avatar');
//        if($file){
//            $info = $file->move( './uploads/avatar');
//            if($info){
//                $data['avatar']='uploads/avatar/'.$info->getSaveName();
//            }else{
//                return false;
//            }
//        }

        return UserModel::update($data,['id'=>$userID]);
    }

    public function index(){
        $userID=self::$userID;
        $info=UserModel::where('id',$userID)->field('username,avatar,phone,email,country')->find()->toArray();
        if($info['avatar']){
            $info['avatar']=get_file_path($info['avatar']);
        }else{
            $info['avatar']=get_file_path(config('data.merchant_avatar_default'));
        }
        $info['id']=$userID+100000;
        return $info;
    }

    public function logout(){
        $handler=Cache::store('redis')->handler();
        $key='USER:'.self::$iat.':'.self::$userID;
        $handler->set($key,self::$jti,config('jwt.access_token_expire'));
        Session::delete('userId');
        Session::delete('userName');
        Session::delete('userType');
        Session::delete('jti');
        Session::delete('iat');
        return true;
    }

    public function logoff($data){
        //验证码校对
        $res=Common::checkCode($data,config('data.code_type.logoff'),config('data.role_type.user'));
        if($res['req']==0){
            return $res;
        }

        //校验手机号是否匹配
        $user_email=UserModel::where(['id'=>self::$userID])->value('email');
        if($user_email!=$data['email']){
            $res['req']=0;
            $res['msg']='The current email does not match the email';
            return $res;
        }

        UserModel::update(['status'=>-1],['id'=>self::$userID]);
        $res['req']=1;
        return $res;
    }

    public function userInfo($id,$field){
        return UserModel::where('id',$id)->field($field)->find()->toArray();
    }

}
